<?php
require_once '../libs/app.config.php';
session_start();
if($_SESSION['LoginId'] == false){
	header("Location:login.php");
}
($charset=='zh_CN')?$charset='cn':null;
	if($id==""){
//无限分类下拉列表
function mainfl()
{ global $show;
	$result=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=0 order by id");
	if ($myrow=mysql_fetch_array($result))
	{
	  do{
		 $show.="<option value=".$myrow['id'].">".$myrow['cat_name']."</option>";
		 subfl($myrow["id"],"&nbsp;");
		}
	  while ($myrow=mysql_fetch_array($result));
	}
}
function subfl($fid,$tag)
{   global $show;
	$result1=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=$fid  order by id");
	if ($myrow1=mysql_fetch_array($result1))
	{
		do {
			$show.="<option value=".$myrow1['id'].">".$tag.$myrow1["cat_name"]."</option>";
			subfl($myrow1["id"],"&nbsp;".$tag);
			}
		while ($myrow1=mysql_fetch_array($result1));
	}
}
mainfl();

	}

	if($_GET['act']=='add')
	{
		$add=array("cat_id"=>$article_cat,
				   "admin_id"=>$_SESSION['LoginId'],
				   "title"=>$title,
				   "content"=>$content,
				   "type"=>$charset
				   );

		$result=$tpm->insert("articles",$add);
		if($result>0)
		echo "<script>alert('Add successfully!');location.href=('news_list.php');</script>";
	}
	//修改分类
	if($id!="")	{
		if($_GET['act']=='edit'){
			$edit = array("cat_id"=>$article_cat,
				   "admin_id"=>$_SESSION['LoginId'],
				   "title"=>$title,
				   "content"=>$content,
				   "type"=>$charset
			);
			$where = sprintf('id=%d',$id);
			$result=$tpm->update("articles",$edit,$where);
			if($result>0)
				echo "<script>alert('Edit successfully!');location.href=('news_list.php');</script>";exit();
		}
	function subfl($fid=0,$tag="&nbsp;")
	{
		global $msg;
		$sql="select id,partent_id,cat_name from article_cat where partent_id=$fid";
		$result1=mysql_query($sql);
		 while ($myrow1=mysql_fetch_array($result1))
		   {
		     $sqls="select * from articles where id='".$_GET['id']."'";
			 $querys=mysql_query($sqls);
			 $row=mysql_fetch_array($querys);
			 if($fid==0){
			   if($row['cat_id']==$myrow1['id'])
			   {$msg.="<option value=$myrow1[id] selected>".$myrow1['cat_name']."</option>"; }
			   else
			   {$msg.="<option value=$myrow1[id]>".$myrow1['cat_name']."</option>"; }
		   	 }
			else{
				   if($row['cat_id']==$myrow1['id'])
			       {$msg.="<option value=$myrow1[id] selected>".$tag.$tag.$myrow1['cat_name']."</option>"; }
			       else
			       {$msg.="<option value=$myrow1[id]>".$tag.$tag.$myrow1['cat_name']."</option>"; }
				}

			subfl($myrow1["id"],"&nbsp;".$tag);

			}
		return $msg;

	}

$msg=subfl();

$sql1="select * from articles where id='".$_GET['id']."'";
$query1=$tpm->query($sql1);
$rows=$tpm->fetch_array($query1);
$list=array("title"=>$rows['title'],
			"content"=>$rows['content'],
			"is_recommend"=>$rows['is_recommend'],
			"is_show"=>$rows['is_show'],
			"type"=>$rows['type'] );
	}



//print_r($list);exit();
require_once(INCLUDE_PATH.'template.inc.php');  //模板类及对象$tpl的加载
    $tpl->assign("list",$list);
	$tpl->assign("id",$id);
    $tpl->assign("msg",$msg);
	$tpl->assign("show",$show);
	$tpl->display("news_add.html");
?>

